package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.BrandListItemAdapter;
import com.sikao.app.bean.BrandListBean;
import com.sikao.app.bean.CreditRecordBean;
import com.sikao.app.bean.GetListSubmitBean;
import com.sikao.app.dao.BrandListModel;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌专区
 */
public class CompanyAreaActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private RefreshListView refreshlistview;
	private BrandListItemAdapter adapter;
	private int type = 0;//0正常加载，1刷新，2加载更多

	private int page = 1;
	private int allPage;
	private BrandListModel brandListModel;
	private GetListSubmitBean bean;
	private  List<BrandListBean.ResBean> beans;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_company_area);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		refreshlistview = (RefreshListView) findViewById(R.id.refreshlistview);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		refreshlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			/*	Intent intent = new Intent(CompanyAreaActivity.this, WebviewActivity.class);//NewsDetailActivity
				*//*Bundle bundle = new Bundle();
				bundle.putParcelable("bean",beans.get(position - 1));*//*
				startActivity(intent);*/
			}
		});
		refreshlistview.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 1;
				type = 1;
				getList(false);
			}

			@Override
			public void onLoadMore() {
				page++;
				type = 2;
				if (page > allPage) {
					Toast.makeText(CompanyAreaActivity.this, "无更多内容", Toast.LENGTH_SHORT).show();
					refreshlistview.onLoadMoreComplete();
				} else {
					getList(false);
				}
			}
		});
	}

	@Override
	protected void init() {
		titleTv.setText("品牌专区");
		backIv.setVisibility(View.VISIBLE);
		beans = new ArrayList<>();
		adapter = new BrandListItemAdapter(this, beans);
		refreshlistview.setAdapter(adapter);
		bean = new GetListSubmitBean();
		brandListModel = new BrandListModel(this, new OnNetBackListener<BrandListBean>() {
			@Override
			public void netBack(BrandListBean data) {
				if (type == 1) {
					refreshlistview.onRefreshComplete();
				}
				if (type == 2) {
					refreshlistview.onLoadMoreComplete();
				}
				if (data == null) {
					return;
				}
				if (data.getStatus()==1) {

					if (type != 2 && beans.size() != 0) {
						beans.clear();
					}
					beans.addAll(data.getRes());
					adapter.notifyDataSetChanged();
				} else {
					Toast.makeText(CompanyAreaActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		});
		getList(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			default:
				break;
		}
	}

	private void getList(boolean isShow) {
		bean.setThisPage(page + "");
		bean.setPageNum("1000");
	    brandListModel.request(JSONUtil.toJSON(bean), isShow);
	}

}
